package com.zhengqing.app.config;

import com.zhengqing.app.model.vo.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * <p> 全局异常处理器 </p>
 *
 * @author zhengqingya
 * @description
 * @date 2024/5/13 17:04
 */
@Slf4j
@RestControllerAdvice
public class MyGlobalExceptionHandler {

    @ExceptionHandler({Exception.class})
    public ApiResult exception(Exception e) {
        log.error("其他错误:", e);
        return ApiResult.fail("其他错误：" + e);
    }

    @ExceptionHandler({MethodArgumentNotValidException.class})
    public ApiResult exception(MethodArgumentNotValidException e) {
        log.error("方法参数校验:", e);
        return ApiResult.fail(e.getBindingResult().getFieldError().getDefaultMessage());
    }

}